package net.etao;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    // 设置环境变量的PS命令 $env:DEEPSEEK_API_KEY="sk-xxxx"
    private static final String API_KEY = System.getenv("DEEPSEEK_API_KEY");
    private static final String BASE_URL = "https://api.deepseek.com/v1";
    public static void main(String[] args) {
        // Java局部变量类型推断（LVTI）: Java 10引入
        // 多行字符串（三引号）: Java 15 引入
        var body = """
                {
        "model": "deepseek-reasoner",
        "messages": [
        {
            "role": "user",
            "content": "英文单词Strawberry中有几个字母r?"
        }
        ]
                }
                """; 
        // 声明式编程
        var request = HttpRequest.newBuilder()
        .uri(URI.create(BASE_URL + "/chat/completions"))
        .header("Content-Type", "application/json")
        .header("Authorization", "Bearer " + API_KEY)
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();
        
        var client = HttpClient.newHttpClient();
        try {
        var response = client.send(request, HttpResponse.BodyHandlers.ofString());
        var responseBody = response.body();    
        
        if (!responseBody.isBlank()) {
            ObjectMapper mapper = new ObjectMapper();
            Object json = mapper.readValue(responseBody, Object.class);
            String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
            System.out.println(prettyJson);
        }
        } catch (Exception e) {}
    }
}